const path = require('path');

function resolve(dir) {
  return path.join(__dirname, dir) 
}

function resolvePages(page){
  let index = {
    entry: 'src/main.js',
    template: 'public/index.html',
    filename: 'index.html',
  }
  let view = {
    entry: 'src/main-view.js',
    template: 'public/index.html',
    filename: 'index.html',
  }
  if(page === "view"){
    return {view}
  }else{
    return {index}
  }
}

module.exports = {
  chainWebpack: (config) => {
    config.resolve.alias
    .set('components',resolve('./src/components'))
    .set('top',resolve('../'))
  },
  devServer: {
    proxy: {
      "/api": {
        target: "http://127.0.0.1:7788", // 要访问的接口域名
        ws: true, // 是否启用websockets
        changeOrigin: true, //开启代理：在本地会创建一个虚拟服务端，然后发送请求的数据，并同时接收请求的数据，这样服务端和服务端进行数据的交互就不会有跨域问题
        pathRewrite: {
          "^/api": "" //这里理解成用'/api'代替target里面的地址,比如我要调用'http://127.0.0.1/user/add'，直接写'/api/user/add'即可
        }
      },
      "/upload":{
        target: "http://127.0.0.1:7788",
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          "^/upload": "" 
        }
      }
    }
  },
  pages:resolvePages(process.env.page)
}